package com.ss.android.article.browser.download.internal;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public final class DownloadDBHelper extends SQLiteOpenHelper {
    public DownloadDBHelper(Context context, String name) {
        super(context, name, null, 1);
    }

    public static ContentValues toContentValues(DownloadTask task) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("url", task.url);
        contentValues.put("downloadState", task.downloadState.toString());
        contentValues.put("file_dir", task.fileDir);
        contentValues.put("filename", task.fileName);
        contentValues.put("title", task.title);
        contentValues.put("thumbnail", task.thumbnail);
        contentValues.put("finishedSize", Long.valueOf(task.finishedSize));
        contentValues.put("totalSize", Long.valueOf(task.totalSize));
        contentValues.put("file_path", task.getFilePath());
        contentValues.put("create_time", Long.valueOf(task.createTime));
        return contentValues;
    }

    public final List<DownloadTask> queryUnfinishedDownloadTask() {
        Cursor cursor = null;
        ArrayList result = new ArrayList();
        try {
            cursor = this.getReadableDatabase().query("download",
                    new String[]{"url", "downloadState", "file_dir", "filename", "title", "thumbnail",
                            "finishedSize", "totalSize", "create_time"}, "downloadState<> \'FINISHED\'",
                    null, null, null, "_id desc");
            if (cursor == null) {
                return result;
            }
            while (true) {
                if (cursor.moveToNext()) {
                    DownloadTask downloadTask = new DownloadTask(cursor.getString(0), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
                    downloadTask.downloadState = DownloadState.valueOf(cursor.getString(1));
                    downloadTask.finishedSize = ((long) cursor.getInt(6));
                    downloadTask.totalSize = ((long) cursor.getInt(7));
                    downloadTask.createTime = cursor.getLong(8);
                    result.add(downloadTask);
                    continue;
                }
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return result;
    }

    public final void onCreate(SQLiteDatabase database) {
        Log.i("DownloadDBHelper", "create download table.");
        StringBuffer sb = new StringBuffer("create table ");
        sb.append("download");
        sb.append("(");
        sb.append("_id");
        sb.append(" integer primary key autoincrement, ");
        sb.append("url");
        sb.append(" text, ");
        sb.append("downloadState");
        sb.append(" text,");
        sb.append("file_dir");
        sb.append(" text, ");
        sb.append("filename");
        sb.append(" text, ");
        sb.append("title");
        sb.append(" text, ");
        sb.append("thumbnail");
        sb.append(" text, ");
        sb.append("finishedSize");
        sb.append(" integer, ");
        sb.append("totalSize");
        sb.append(" integer,");
        sb.append("file_path");
        sb.append(" text unique,");
        sb.append("create_time");
        sb.append(" integer )");
        String sql = sb.toString();
        Log.i("DownloadDBHelper", sql);
        database.execSQL(sql);
    }

    public final void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
    }
}

